/********
 *
 *Awais
 *November 29,2011
 *edited css from: http://www.queness.com/post/1696/create-a-beautiful-looking-custom-dialog-box-with-jquery-and-css3
 *
*/
(function( $ ) {
    
    var logSettings={};

    var logContainer=this;
    
    var helper={
        objThis:{},
        hideIt:function(){
            if(logSettings.modal){
                $("#dialog-overlay").hide();
            }
            this.objThis.fadeOut();
        },
        hideIt:function(time){
            if(logSettings.modal){
                $("#dialog-overlay").hide();
            }
            this.objThis.fadeOut(time);
        }
    }

    var methods = {
        init : function( options ) {
            helper.objThis=this;
            var maskHeight = $(document).height();
            var maskWidth = $(window).width();

            // calculate the values for center alignment
            var dialogTop =  (maskHeight/3) - (this.height());
            var dialogLeft = (maskWidth/2) - (this.width()/2);
            
            logSettings = $.extend({
                'modal':false,
                'width':300,
                'height':100,
                'top':dialogTop,
                'left':dialogLeft,
                'titleBar':true,
                'title':"Log",
                'cancelButton':true,
                'messageSelector':"#dialog-message",
                'contentSelector':"#dialog-content",
                'maskHeight':$(document).height(),
                'maskWidth':$(window).width(),
                'autoHide':false,
                'timeToHide':1500
            }, options);

            var appendHtml='<div class="dialog-content"><ul id="dialog-message"></ul></div>';
            this.html(appendHtml);
            this.before('<div id="dialog-overlay"></div>');
            this.prepend("<div class='titleBar'><div class='title'>"+logSettings.title+"</div></div><div style='clear:both'></div>");
            var that=this;
            var temp=this.children(".titleBar:first");
            temp.append("<a class='button'>x</a>");
            
            
            //$(logSettings.contentSelector).append("<a class='button'>x</a>");
            //alert(logSettings.contentSelector);

            /*$(logSettings.contentSelector).height(logSettings.height-20);
            $(logSettings.contentSelector).width(logSettings.width-20);*/
           
            this.width(logSettings.width);
            this.height(logSettings.height);

            if(logSettings.titleBar){
                $(logSettings.messageSelector).height(this.innerHeight()-40);
            }
            else{
                
                $(logSettings.messageSelector).height(this.innerHeight()-20);
            //$(logSettings.messageSelector).width(this.innerWidth());
            }
            
        },
        show : function( ) {

            this.width(logSettings.width);
            this.height(logSettings.height);

            if(logSettings.modal){
                $("#dialog-overlay").css({
                    height:logSettings.maskHeight,
                    width:logSettings.maskWidth
                }).show();
            }
            var temp=this.children(".titleBar:first");
            var that=this;
            var bt=temp.children(".button:first");
            if(logSettings.titleBar){
                temp.show();
                if(logSettings.cancelButton){
                    bt.show();
                    bt.unbind("click");
                    bt.click(function(){
                        helper.hideIt(300);
                    });
                }
                else{
                    bt.unbind("click");
                    bt.hide();
                }
            }
            else{
                temp.hide();
            }
            this.css({
                top:logSettings.top,
                left:logSettings.left
            }).show();

        },
        hide : function( ) {
            helper.hideIt();
        },
        hide : function(time){
            helper.hideIt(time);
        },
        update : function( content ) {
        
        },
        msg:function(msg){
            $(logSettings.messageSelector).prepend("<li>"+msg+"</li>");
            $(logSettings.messageSelector+" li").filter(":first").slideDown();
            
            if(logSettings.autoHide){
                setTimeout(function(){
                    helper.hideIt(300);
                },logSettings.timeToHide);
            }
            
        },
        err:function(err){
			/*var now = new Date();
			var Time = now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();*/
            $(logSettings.messageSelector).prepend("<li class='err'>"+err+"</li>");
            $(logSettings.messageSelector+" li").filter(":first").slideDown();

            if(logSettings.autoHide){
                setTimeout(function(){
                    helper.hideIt(300);
                },logSettings.timeToHide);
            }
            
        },
        clear:function(){
            $(logSettings.messageSelector).html("");
        },
        setTitle:function(title){
            var temp=this.children(".titleBar:first");
            temp=temp.children(".title:first");
            temp.html(title);
            logSettings.title=title;
        },
        setTitleBar:function(bool){
            
            logSettings.titleBar=bool;
        },
        setCancelButton:function(bool){
            logSettings.cancelButton=bool;
        },
        setAutoHide:function(bool){
            logSettings.autoHide=bool;
        },
        setTimeToHide:function(tim){
            logSettings.timeToHide=tim;
        },
        setTop:function(top){
            logSettings.top=top;
        },
        setLeft:function(left){
            logSettings.left=left;
        },
        setWidth:function(w){
            logSettings.width=w;
        },
        setHeight:function(h){
            logSettings.height=h;
        },
		centerAlign:function(){
			var maskHeight = $(document).height();
            var maskWidth = $(window).width();

            // calculate the values for center alignment
            var dialogTop =  (maskHeight/3) - (this.height());
            var dialogLeft = (maskWidth/2) - (this.width()/2);
			
			logSettings.top=dialogTop;
			logSettings.left=dialogLeft;
		}
    };

    
    $.fn.myLog = function( method ) {

        // Method calling logic
        if ( methods[method] ) {
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
        }
        
    };
    
})( jQuery );